package com.smartwasser.yunhtgl.apphtgl.activity;

import android.app.Dialog;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.LoginBean;
import com.smartwasser.yunhtgl.apphtgl.presenter.LoginPresenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.LoginPresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.view.LoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.smartwasser.yunhtgl.apphtgl.R.id.login;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.md5;
import static com.smartwasser.yunhtgl.apphtgl.utils.ResultUtils.successCode;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getBooleanData;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.setBooleanDate;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.setStringData;
import static com.smartwasser.yunhtgl.apphtgl.widget.CustomToast.showToast;

/**
 * 登陆
 */
public class LoginActivity extends BaseActivity implements LoginView {
    private LoginPresenter mLoginPresenter = new LoginPresenterImpl(this);
    private Handler handler = new Handler();
    private Dialog loadingHorizontal;
    private Boolean isLoginFail;
    @BindView(R.id.checkbox_login)
    CheckBox checkbox_login;
    @BindView(R.id.login_name)
    EditText login_name;
    @BindView(R.id.login_password)
    EditText login_password;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        isLoginFail = getIntent().getBooleanExtra("isLoginFail", false);
        if (getBooleanData(this, "isChecked", false)) {
            checkbox_login.setChecked(true);
            login_password.setText(getStringData(this, "login_password", ""));
        }
        login_name.setText(getStringData(this, "login_name", ""));
        checkbox_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton com, boolean isChecked) {
                setBooleanDate(getApplication(), "isChecked", isChecked);
            }
        });
    }

    @Override
    public void initData() {
    }

    @OnClick({R.id.login,R.id.login_register})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                String username = login_name.getText().toString();
                String password = login_password.getText().toString();

                if (username.length() > 0) {

                    if (password.length() > 0) {
                        loadingHorizontal = showLoadingHorizontal(this, "登陆中...").show();
                        setStringData(getApplication(), "login_name", username);
                        if (getBooleanData(this, "isChecked", false)) {
                            setStringData(this, "login_password", password);
                        } else {
                            setStringData(this, "login_password", password);
                            setBooleanDate(LoginActivity.this, "isChecked", false);
                        }
                        try {
                            mLoginPresenter.login(username, md5(password));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } else {
                        showToast(this, "请输入密码");
                    }

                } else {
                    showToast(this, "请输入用户名");
                }
                break;
            case R.id.login_register:
                startActivity(RegisterActivity.class);
                break;
        }
    }

    @Override
    public void onSuccess(LoginBean loginBean) {
        dismiss(loadingHorizontal);
        if (successCode(loginBean.getResultCode())) {
            setStringData(this, "JSESSIONID", loginBean.getJSESSIONID());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isLoginFail) {
                        startActivity(SysActivity.class);
                    } else {
                        startActivity(GestureSetActivity.class);
                    }
                    finish();
                }
            }, 1000);
        } else {
            showToast(this, loginBean.getMessage());
        }
    }

    @Override
    public void OnError() {
        dismiss(loadingHorizontal);
        showError();
    }
}
